package com.edu.lawdoc.api.interceptor;

import com.edu.lawdoc.constant.Const;
import com.edu.lawdoc.pvg.Pvg;
import com.edu.lawdoc.pvg.PvgUser;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

import javax.servlet.http.HttpServletResponse;

public class PvgInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation invocation) {
        Controller controller = invocation.getController();
        HttpServletResponse response = controller.getResponse();
        System.out.println("controller.getHeader(\"Origin\") => " + controller.getHeader("Origin"));
        /** 支持跨域请求 */
        //response.setHeader("Access-Control-Allow-Origin",controller.getHeader("Origin"));
        //response.setHeader("Access-Control-Allow-Methods", "*");
        /** 设置Access-Control-Allow-Credentials属性为true使sessionId保持不变。*/
        //response.setHeader("Access-Control-Allow-Credentials", "true");
        //response.setHeader("Access-Control-Allow-Headers", "Authorization,Origin, X-Requested-With, Content-Type, Accept,Access-Token");//Origin, X-Requested-With, Content-Type, Accept,Access-Token

        PvgUser pvg = controller.getSessionAttr(Const.pvg.name());
        if(pvg == null) {
            System.out.println("New Session");
            pvg = new PvgUser();
            pvg.setName("xiao.hu");
            pvg.setNameCn("胡晓");
            pvg.setUserId(1L);
            controller.setSessionAttr(Const.pvg.name(), pvg);
        }

        Pvg.setPvg(pvg);
        invocation.invoke();
        Pvg.removePvg();
    }
}
